package org.dromara.merchants.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.merchants.domain.vo.TaCustomClassificationVo;
import org.dromara.merchants.domain.vo.TaGroupGoodsVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.merchants.domain.vo.TaGoodsVo;
import org.dromara.merchants.domain.bo.TaGoodsBo;
import org.dromara.merchants.service.ITaGoodsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 商品
 * 前端访问路由地址为:/merchants/goods
 *
 * @author xiyin321
 * @date 2024-07-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/goods")
public class TaGoodsController extends BaseController {

    private final ITaGoodsService taGoodsService;

    /**
     * 查询商品列表
     */
//    @SaCheckPermission("merchants:goods:list")
    @GetMapping("/list")
    public TableDataInfo<TaGoodsVo> list(TaGoodsBo bo, PageQuery pageQuery) {
        return taGoodsService.queryPageList(bo, pageQuery);
    }

    /**
     * app端查询商品列表
     * @param bo
     * @param pageQuery
     * @return
     */
    @GetMapping("/appList")
    public TableDataInfo<TaGoodsVo> appList(TaGoodsBo bo, PageQuery pageQuery) {
        return taGoodsService.queryAppList(bo, pageQuery);
    }
    /**
     * app端分组查询商品列表
     * @param bo
     * @param pageQuery
     * @return
     */
    @GetMapping("/appGroupList")
    public TableDataInfo<TaGroupGoodsVo> appGroupList(TaGoodsBo bo, PageQuery pageQuery) {
        return taGoodsService.queryAppGroupList(bo, pageQuery);
    }
    /**
     * 查询商品下拉列表
     */
    @GetMapping("/option")
    public R<List<TaGoodsVo>> option(TaGoodsBo bo) {
        return R.ok(taGoodsService.queryOptionList(bo));
    }
    /**
     * 导出商品列表
     */
//    @SaCheckPermission("merchants:goods:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TaGoodsBo bo, HttpServletResponse response) {
        List<TaGoodsVo> list = taGoodsService.queryList(bo);
        ExcelUtil.exportExcel(list, "商品", TaGoodsVo.class, response);
    }

    /**
     * 获取商品详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<TaGoodsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(taGoodsService.queryById(id));
    }

    /**
     * 新增商品
     */
//    @SaCheckPermission("merchants:goods:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TaGoodsBo bo) {
        return toAjax(taGoodsService.insertByBo(bo));
    }

    /**
     * 修改商品
     */
//    @SaCheckPermission("merchants:goods:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TaGoodsBo bo) {
        return toAjax(taGoodsService.updateByBo(bo));
    }

    /**
     * 删除商品
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("merchants:goods:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(taGoodsService.deleteWithValidByIds(List.of(ids), true));
    }
}
